<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎注册</title>
    <link href="css/register.css" rel="stylesheet">
</head>
<body>

<div class="form-div">
    <div class="reg-content">
        <h1>欢迎注册</h1>
        <span>已有帐号？</span> <a href="login.jsp">登录</a>
    </div>
    <form id="reg-form" action="/teaching/registerServlet" method="post">

        <table>

            <tr>
                <td>用户名</td>
                <td class="inputs">
                    <input name="username" type="text" id="username" >
                    <br>
                    <span id="name_err" class="err_msg" style="display: none">用户名为12个以内的字母数字</span>
                    <span id="username_err" class="err_msg" >${register_msg}</span>
                </td>
            </tr>

            <tr>
                <td>密码</td>
                <td class="inputs">
                    <input name="password" type="password" id="password">
                    <br>
                    <span id="password_err" class="err_msg" style="display: none">密码为6个以内的字母数字</span>
                </td>
            </tr>


            <tr>
                <td>验证码</td>
                <td class="inputs">
                    <input name="checkCode" type="text" id="checkCode">
                    <img id="checkCodeImg" src="/teaching/checkCodeServlet">
                    <a href="#" id="changeImg">看不清？</a>
                </td>
            </tr>

        </table>

        <div class="buttons">
            <input value="注 册" type="submit" id="reg_btn">
        </div>
        <br class="clear">
    </form>

</div>
<script>
    document.getElementById("changeImg").onclick = function () {
        document.getElementById("checkCodeImg").src = "/teaching/checkCodeServlet?"+new Date().getMilliseconds();
    }

    document.getElementById("checkCodeImg").onclick = function () {
        document.getElementById("checkCodeImg").src = "/teaching/checkCodeServlet?"+new Date().getMilliseconds();
    }

    //给用户名绑定失去焦点事件
    var usernameInput = document.getElementById("username");
    usernameInput.onblur = checkUsername;

        function checkUsername() {
        //获取用户名
        var username = usernameInput.value.trim();
        var reg = /^\w{1,12}$/;
        var flag = reg.test(username);

        //用户名为12个以内的字符，可以说字母数字
        if(flag){
            document.getElementById("name_err").style.display = 'none';
        }else {
            document.getElementById("name_err").style.display = '';
        }
        return flag;
    }

    //给密码绑定失去焦点事件
    var passwordInput = document.getElementById("password");
    passwordInput.onblur = checkPassword;

        function checkPassword() {
        //获取密码
        var password = passwordInput.value.trim();
        var reg = /^\w{1,6}$/;
        var flag = reg.test(password);

        //密码为6个以内的字符，可以是字母数字
        if(flag){
            document.getElementById("password_err").style.display = 'none';
        }else {
            document.getElementById("password_err").style.display = '';
        }
        return flag;
    }


    //获取表单对象
    var regForm = document.getElementById("reg-form");

    regForm.onsubmit = function () {
        var flag = checkUsername()&&checkPassword();

        //返回true时，表单才能被提交
        return flag;
    }

</script>
</body>
</html>